package client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.HashSet;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;

public class BookmarksSelector extends JFrame {
	private static final long serialVersionUID = -1407416907810342715L;
	
	private HashSet<String> bookmarks;
	private JButton bookmarkButton;
	private AdvancedBrowser advdbrs;
	
	public BookmarksSelector(HashSet<String> bookmarks, final AdvancedBrowser advdbrs) {
		this.bookmarks = bookmarks;
		this.advdbrs = advdbrs;
		setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		
		setSize(800, 600);
		
		
		for (String bkm : bookmarks) {
			JLabel link = new JLabel(bkm);
			link.addMouseListener(new DoubleCLickListener(bkm));
			add(link);
		}
		
		bookmarkButton = new JButton("Bookmark");
		bookmarkButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				advdbrs.bookmark();
			}
		});
		add(bookmarkButton);
		pack();
		
		setVisible(true);
	}
	
	class DoubleCLickListener implements MouseListener {
		
		private String url;
		
		public DoubleCLickListener(String link) {
			url = link;
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			if (e.getClickCount() == 2) {
				advdbrs.setUrl(url);
				dispose();
			}
		}

		@Override
		public void mousePressed(MouseEvent e) {}

		@Override
		public void mouseReleased(MouseEvent e) {}

		@Override
		public void mouseEntered(MouseEvent e) {}

		@Override
		public void mouseExited(MouseEvent e) {}
	}
	

}
